package com.sjgs.gis.web.rest;

import com.sjgs.gis.service.JdbcClusterService;
import com.sjgs.gis.web.errors.BadRequestAlertException;
import com.sjgs.gis.service.dto.JdbcClusterDTO;
import com.sjgs.gis.service.dto.JdbcClusterCriteria;
import com.sjgs.gis.service.JdbcClusterQueryService;

import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.sjgs.gis.domain.JdbcCluster}.
 */
@RestController
@RequestMapping("/api")
public class JdbcClusterResource {

    private final Logger log = LoggerFactory.getLogger(JdbcClusterResource.class);

    private static final String ENTITY_NAME = "geoserverportalJdbcCluster";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final JdbcClusterService jdbcClusterService;

    private final JdbcClusterQueryService jdbcClusterQueryService;

    public JdbcClusterResource(JdbcClusterService jdbcClusterService, JdbcClusterQueryService jdbcClusterQueryService) {
        this.jdbcClusterService = jdbcClusterService;
        this.jdbcClusterQueryService = jdbcClusterQueryService;
    }

    /**
     * {@code POST  /jdbc-clusters} : Create a new jdbcCluster.
     *
     * @param jdbcClusterDTO the jdbcClusterDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new jdbcClusterDTO, or with status {@code 400 (Bad Request)} if the jdbcCluster has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/jdbc-clusters")
    public ResponseEntity<JdbcClusterDTO> createJdbcCluster(@Valid @RequestBody JdbcClusterDTO jdbcClusterDTO) throws URISyntaxException {
        log.debug("REST request to save JdbcCluster : {}", jdbcClusterDTO);
        if (jdbcClusterDTO.getId() != null) {
            throw new BadRequestAlertException("A new jdbcCluster cannot already have an ID", ENTITY_NAME, "idexists");
        }
        JdbcClusterDTO result = jdbcClusterService.save(jdbcClusterDTO);
        return ResponseEntity.created(new URI("/api/jdbc-clusters/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /jdbc-clusters} : Updates an existing jdbcCluster.
     *
     * @param jdbcClusterDTO the jdbcClusterDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated jdbcClusterDTO,
     * or with status {@code 400 (Bad Request)} if the jdbcClusterDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the jdbcClusterDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/jdbc-clusters")
    public ResponseEntity<JdbcClusterDTO> updateJdbcCluster(@Valid @RequestBody JdbcClusterDTO jdbcClusterDTO) throws URISyntaxException {
        log.debug("REST request to update JdbcCluster : {}", jdbcClusterDTO);
        if (jdbcClusterDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        JdbcClusterDTO result = jdbcClusterService.save(jdbcClusterDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, jdbcClusterDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /jdbc-clusters} : get all the jdbcClusters.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of jdbcClusters in body.
     */
    @GetMapping("/jdbc-clusters")
    public ResponseEntity<List<JdbcClusterDTO>> getAllJdbcClusters(JdbcClusterCriteria criteria, Pageable pageable) {
        log.debug("REST request to get JdbcClusters by criteria: {}", criteria);
        Page<JdbcClusterDTO> page = jdbcClusterQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * {@code GET  /jdbc-clusters/count} : count all the jdbcClusters.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/jdbc-clusters/count")
    public ResponseEntity<Long> countJdbcClusters(JdbcClusterCriteria criteria) {
        log.debug("REST request to count JdbcClusters by criteria: {}", criteria);
        return ResponseEntity.ok().body(jdbcClusterQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /jdbc-clusters/:id} : get the "id" jdbcCluster.
     *
     * @param id the id of the jdbcClusterDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the jdbcClusterDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/jdbc-clusters/{id}")
    public ResponseEntity<JdbcClusterDTO> getJdbcCluster(@PathVariable Long id) {
        log.debug("REST request to get JdbcCluster : {}", id);
        Optional<JdbcClusterDTO> jdbcClusterDTO = jdbcClusterService.findOne(id);
        return ResponseUtil.wrapOrNotFound(jdbcClusterDTO);
    }

    /**
     * {@code DELETE  /jdbc-clusters/:id} : delete the "id" jdbcCluster.
     *
     * @param id the id of the jdbcClusterDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/jdbc-clusters/{id}")
    public ResponseEntity<Void> deleteJdbcCluster(@PathVariable Long id) {
        log.debug("REST request to delete JdbcCluster : {}", id);
        jdbcClusterService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString())).build();
    }
}
